package com.example;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;

public class AdminUserDialogController {
    @FXML private TextField usernameField;
    @FXML private TextField nameField;
    @FXML private ComboBox<String> typeComboBox;
    @FXML private TextField departmentField;
    @FXML private ComboBox<String> statusComboBox;
    @FXML private PasswordField passwordField;

    private Stage dialogStage;
    private User user;
    private boolean confirmed = false;

    @FXML
    public void initialize() {
        // 初始化用户类型下拉框
        typeComboBox.setItems(FXCollections.observableArrayList("教师", "学生"));
        typeComboBox.getSelectionModel().selectFirst();

        // 初始化状态下拉框
        statusComboBox.setItems(FXCollections.observableArrayList("正常", "禁用"));
        statusComboBox.getSelectionModel().selectFirst();
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public void setUser(User user) {
        this.user = user;

        if (user != null) {
            // 编辑模式：填充现有用户数据
            usernameField.setText(user.getUsername());
            usernameField.setEditable(false);
            nameField.setText(user.getName());
            typeComboBox.setValue(user.getType());
            departmentField.setText(user.getDepartment());
            statusComboBox.setValue(user.getStatus());
            passwordField.setText("");
        } else {
            // 添加模式：用户名可编辑
            usernameField.setEditable(true);
            passwordField.setText("");
        }
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public User getUser() {
        if (user == null) {
            // 在添加模式下，使用正确的6参数构造器
            return new User(
                usernameField.getText(),
                nameField.getText(),
                typeComboBox.getValue(),
                departmentField.getText(),
                statusComboBox.getValue(),
                passwordField.getText()
            );
        } else {
            // 编辑模式下，只更新密码字段（如果输入了新密码），其他字段由AdminUserManagementController处理
            String updatedPassword = passwordField.getText();
            if (updatedPassword != null && !updatedPassword.trim().isEmpty()) {
                user.setPassword(updatedPassword.trim());
            }
            return user;
        }
    }

    @FXML
    private void handleConfirm() {
        if (isInputValid()) {
            confirmed = true;
            dialogStage.close();
        }
    }

    @FXML
    private void handleCancel() {
        dialogStage.close();
    }

    private boolean isInputValid() {
        String errorMessage = "";

        if (usernameField.getText() == null || usernameField.getText().trim().isEmpty()) {
            errorMessage += "用户名不能为空\n";
        }
        if (nameField.getText() == null || nameField.getText().trim().isEmpty()) {
            errorMessage += "姓名不能为空\n";
        }
        if (departmentField.getText() == null || departmentField.getText().trim().isEmpty()) {
            errorMessage += "院系不能为空\n";
        }

        if (user == null) {
            if (passwordField.getText() == null || passwordField.getText().trim().isEmpty()) {
                errorMessage += "密码不能为空\n";
            }
        }

        if (errorMessage.length() == 0) {
            return true;
        } else {
            // 显示错误消息
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("输入错误");
            alert.setHeaderText(null);
            alert.setContentText(errorMessage);
            alert.showAndWait();
            return false;
        }
    }
} 